#include "ServerManage.h"

ServerManage::ServerManage(wxEvtHandler* evtHandler,int id) :
    serverHandle(evtHandler,id),serverCore(&serverHandle)
{
    networkThread=std::thread(&ServerCore::operator(),&serverCore);
}

ServerManage::~ServerManage()
{
    serverCore.StopServer();
    if(networkThread.joinable())
        networkThread.join();
}

void ServerManage::SetDevice(long devID)
{
    if(CurrentDeviceID==-1)
    {
        CurrentDeviceID=devID;
        serverHandle.StartCollegeImage(CurrentDeviceID);
        return;
    }
    if(devID==-1)
    {
      CurrentDeviceID=devID;
      return;
    }
    serverHandle.StopCollegeImage(CurrentDeviceID);
    CurrentDeviceID=devID;
    serverHandle.StartCollegeImage(CurrentDeviceID);
}

void ServerManage::InitDevice()
{
    if(CurrentDeviceID==-1) return;
    serverHandle.InitDevice(CurrentDeviceID);
}

DeviceStatus ServerManage::GetDeviceInfo()
{
    if(CurrentDeviceID==-1) return(DeviceStatus::deviceError);
    return (DeviceStatus)(serverHandle.GetDeviceInfo(CurrentDeviceID));
}

void ServerManage::ControlCollege()
{
    if(CurrentDeviceID==-1) return;
    DeviceStatus deviceStatus=GetDeviceInfo();
    if(deviceStatus==DeviceStatus::startShot)
    {
        serverHandle.StartCollege(CurrentDeviceID);
    }
    else if(deviceStatus==DeviceStatus::startCollege)
    {
        serverHandle.StopCollege(CurrentDeviceID);
    }
    else
    {
        throw ServerManageException(fmt::format("设备状态错误,当前状态为:{}",(int)deviceStatus));
    }
    deviceStatus=GetDeviceInfo();
    if(deviceStatus==DeviceStatus::deviceError)
        throw ServerManageException("指令执行错误");
}

void ServerManage::ControlShot()
{
    if(CurrentDeviceID==-1) return;
    DeviceStatus deviceStatus=GetDeviceInfo();
    if(deviceStatus==DeviceStatus::openCamera)
    {
        serverHandle.StartShot(CurrentDeviceID);
    }
    else if(deviceStatus==DeviceStatus::startShot)
    {
        serverHandle.StopShot(CurrentDeviceID);
    }
    else
    {
        throw ServerManageException(fmt::format("设备状态错误,当前状态为:{}",(int)deviceStatus));
    }
    deviceStatus=GetDeviceInfo();
    if(deviceStatus==DeviceStatus::deviceError)
        throw ServerManageException("指令执行错误");
}

CameraParament ServerManage::GetCameraPara()
{
    DeviceStatus deviceStatus=GetDeviceInfo();
    if(deviceStatus!=DeviceStatus::openCamera)
        throw ServerManageException(fmt::format("设备状态错误,当前状态为:{}",(int)deviceStatus));
    return serverHandle.GetCameraPara(CurrentDeviceID);
}

void ServerManage::SetTriggerMode(bool mode)
{
    DeviceStatus deviceStatus=GetDeviceInfo();
    if(deviceStatus!=DeviceStatus::openCamera)
        throw ServerManageException(fmt::format("设备状态错误,当前状态为:{}",(int)deviceStatus));
    if(mode)
        serverHandle.EnableTriggerMode(CurrentDeviceID);
    else
        serverHandle.DisableTriggerMode(CurrentDeviceID);
}

void ServerManage::SetExposeTime(int32_t value)
{
    if(CurrentDeviceID==-1) return;
    DeviceStatus deviceStatus=GetDeviceInfo();
    if(deviceStatus==DeviceStatus::openCamera)
    {
        serverHandle.SetExposeTime(CurrentDeviceID,value);
    }
}

void ServerManage::SetImageHeight(const int32_t value)
{
    if(CurrentDeviceID==-1) return;
    DeviceStatus deviceStatus=GetDeviceInfo();
    if(deviceStatus==DeviceStatus::openCamera)
    {
        serverHandle.SetImageHeight(CurrentDeviceID,value);
    }
}

void ServerManage::SetImageWidth(const int32_t value)
{
    if(CurrentDeviceID==-1) return;
    DeviceStatus deviceStatus=GetDeviceInfo();
    if(deviceStatus==DeviceStatus::openCamera)
    {
        serverHandle.SetImageWidth(CurrentDeviceID,value);
    }
}

void ServerManage::SetImageOffsetX(const int32_t value)
{
    if(CurrentDeviceID==-1) return;
    DeviceStatus deviceStatus=GetDeviceInfo();
    if(deviceStatus==DeviceStatus::openCamera)
    {
        serverHandle.SetImageOffsetX(CurrentDeviceID,value);
    }
}

void ServerManage::SetImageOffsetY(const int32_t value)
{
    if(CurrentDeviceID==-1) return;
    DeviceStatus deviceStatus=GetDeviceInfo();
    if(deviceStatus==DeviceStatus::openCamera)
    {
        serverHandle.SetImageOffsetY(CurrentDeviceID,value);
    }
}

void ServerManage::SetFrameRate(const int32_t value)
{
    if(CurrentDeviceID==-1) return;
    DeviceStatus deviceStatus=GetDeviceInfo();
    if(deviceStatus==DeviceStatus::openCamera)
    {
        serverHandle.SetFrameRate(CurrentDeviceID,value);
    }
}

bool ServerManage::FileTransferFunc(const std::string& savePath)
{
    if(CurrentDeviceID==-1) return false;
    return serverHandle.FileTransferFunc(CurrentDeviceID,savePath);
}
